<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');
session_start();

class user extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->library('upload');
		$this->load->library('pagination');
		$this->load->library('parser');
		$this->load->helper('cookie');
		$this->load->helper(array('form', 'url'));
		@session_start();
	}

	public function index(){
		$data['error'] = "";
		$this->load->view('admin/register',$data);
	}

	public function dashboard(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
				$this->load->model('user_model');
				$this->load->view('user/dashboard');

		}
	}

	public function register($error=null){

		if (isset($_REQUEST['registersmb'])) {
			$name = $this->input->post('usernamesignup', true);
			$phone = $this->input->post('phonesignup', true);
			$pass = $this->input->post('passwordsignup', true);
			$pass2 = $this->input->post('passwordsignup_confirm', true);
			if($pass <> $pass2){
				redirect('user/register/pass');
			}else{
				$this->load->model('user_model');
				$result = $this->user_model->_adduser($name,$pass,$phone );
				if($result == 1){
					redirect('user/login/');
				}else{
					redirect('user/register/exit');
				}
			}

		}
		if($error == "pass"){
			$data['error'] = "Mật khẩu chưa trùng khớp";
		}elseif($error=="exit"){
			$data['error'] = "Tài khoản đã tồn tại";
		}else{
			$data['error'] = "";
		}

		$this->load->view('admin/register',$data);
	}

	public function login($error=null) {
		if (isset($_REQUEST['loginsmb'])) {
			$username = $this->input->post('uname', true);
			$pass1 = $this->input->post('pass1', true);
			$this->load->model('user_model');
			$result = $this->user_model->check_login_customer($username, $pass1);

			if ($result == null) {
				redirect('user/login/' . rand(1, 10));
			} else {
				$newdata = array(
                    'userid' => $result,
                    'username' => $username
				);
				$this->session->set_userdata($newdata);
				redirect('user/dashboard');
			}
		}
		if($error <> null){
			$data['error'] = "Mật khẩu hoặc tên đăng nhập không đúng";
		}else{
			$data['error'] = "";
		}


		$this->load->view('admin/register',$data);
	}

	public function logout() {
		$this->session->unset_userdata('userid');
		$this->session->unset_userdata('username');
		redirect('user/register');
	}


	public function app(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$userid =$this->session->userdata('userid');
			$data['apps'] = $this->app_model->list_app_user($userid);
			$this->load->view('user/dashboard',$data);
		}
	}

	public function add_app(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			if (isset($_REQUEST['submit'])) {
				$appname = $this->input->post('appname', true);
				$applink = $this->input->post('applink', true);
				$active = $this->input->post('active', true);
				$userid =  $this->session->userdata('userid');
				/*
				 * -------- Tao folder cho user
				*/
				$user_folder = './app/'.$userid;
				if( is_dir($user_folder) === false )
				{
					mkdir($user_folder, 0777,true);
					$mypath = $user_folder;
				}else{
					$mypath = base_url('app/'.$userid);
				}
				$ifile = $this->do_upload_file('./app/'.$userid, 'ifile');
				$link = $mypath.'/'.$ifile;

				if($applink ==null){
					$applink = $link;
				}
				//----------------------------------
				$this->app_model->add_app($userid,$appname,$applink,$active);
				redirect('user/app');
			}
			$this->load->view('user/dashboard');
		}
	}

	public function update_app($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$userid =  $this->session->userdata('userid');
			if (isset($_REQUEST['submit'])) {
				$appname = $this->input->post('appname', true);
				$applink = $this->input->post('applink', true);
				$active = $this->input->post('active', true);
				/*
				 * -------- Tao folder cho user
				 */
				$user_folder = './app/'.$userid;
				if( is_dir($user_folder) === false )
				{
					mkdir($user_folder, 0777);
				}

				$mypath = $user_folder;
				$ifile = $this->do_upload_file($mypath, 'ifile');
				$link = base_url('app/'.$userid.'/'.$ifile);
				if($applink ==null){
					$applink = $link;
				}
				//----------------------------------
				$this->app_model->update_app($id,$userid,$appname,$applink,$active);
				redirect('user/app');
			}
			$data['apps'] = $this->app_model->get_app($userid,$id);
			$this->load->view('user/dashboard',$data);
		}
	}

	public function del_app($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$userid =  $this->session->userdata('userid');
// 			$app_link = $this->app_model->return_app_name($userid,$id);
// 			unlink(base_url('app/'.$userid.'/'.$app_link));
			$this->app_model->del_app($userid,$id);
			redirect('user/app');
		}
	}


	public function api(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$this->load->model('api_model');
			$this->load->model('shortcode_model');
			$data['apps'] = $this->app_model->list_app_user($this->session->userdata('userid'));
			$data['apis'] = $this->api_model->list_api_user($this->session->userdata('userid'));
			$userid =  $this->session->userdata('userid');
			$data['userid'] = $userid;
			$data['shortcode'] = $this->shortcode_model->list_shortcode();
			$this->load->view('user/dashboard',$data);
		}
	}

	public function add_api(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$this->load->model('api_model');
			$this->load->model('shortcode_model');
			if (isset($_REQUEST['submit'])) {
				$apiid = $this->input->post('app_id', true);
				$mess = $this->input->post('mess', true);
				$cost = $this->input->post('cost', true);
				$active = $this->input->post('active', true);
				$userid =  $this->session->userdata('userid');
				$this->api_model->add_api($userid,$apiid,$mess,$cost,$active);
				redirect('user/api');
			}
			$data['apps'] = $this->app_model->list_app_user($this->session->userdata('userid'));
			$data['shortcode'] = $this->shortcode_model->list_shortcode();
			$this->load->view('user/dashboard',$data);
		}
	}

	public function update_api($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$this->load->model('api_model');
			$this->load->model('shortcode_model');
			$userid =  $this->session->userdata('userid');
			if (isset($_REQUEST['submit'])) {
				$apiid = $this->input->post('app_id', true);
				$mess = $this->input->post('mess', true);
				$cost = $this->input->post('cost' );
				$active = $this->input->post('active', true);
				$this->api_model->update_api($id,$userid,$apiid,$mess,$cost,$active);
				redirect('user/api');
			}
			$data['apps'] = $this->app_model->list_app_user($this->session->userdata('userid'));
			$data['apis'] = $this->api_model->get_api($userid,$id);
			$data['shortcode'] = $this->shortcode_model->list_shortcode();
			$this->load->view('user/dashboard',$data);
		}
	}


	public function del_api($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('api_model');
			$userid =  $this->session->userdata('userid');
			$this->api_model->del_api($userid,$id);
			redirect('user/api');
		}
	}

	public function report() {
		if ($this->session->userdata ( 'userid' ) == null) {
			redirect ( 'user/login' );
		} else {
			$this->load->model ( 'report_model' );
			$userid =$this->session->userdata('userid');
			$data['list_sms'] = $this->report_model->list_sms_by_user($userid);

			$data['profit'] = $this->report_model->total_profit($userid);

			$this->load->view ( 'user/dashboard',$data );
		}
	}

	public function profile(){
	if ($this->session->userdata ( 'userid' ) == null) {
			redirect ( 'user/login' );
		} else {
			$this->load->model ( 'report_model' );
			$this->load->model('user_model');
			$userid =$this->session->userdata('userid');
			$data['list_sms'] = $this->report_model->list_sms_by_user($userid);
			$data['profit'] = $this->report_model->total_profit($userid);
			$data['profiles'] = $this->user_model->show_details_user($userid);
			$this->load->view ( 'user/dashboard',$data );
		}
	}



	/*
	 * ------------------------------------------------------------------------------------------
	 */


	function do_upload_file($mypath, $filename) {
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'jar|jad|apk|ipa';
		$config['max_size'] = '500000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if (isset($filename)) {
			//echo $filename; die();
			if (!$this->upload->do_upload($filename)) {
				$error = array('error' => $this->upload->display_errors());
				//print_r($error);
				return null;
			} else {
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		} else {
			echo $this->upload->display_errors();
		}
	}

}